#include "slidingverificationdialog.h"
#include "ui_slidingverificationdialog.h"


#include <QImage>
#include <QPixmap>
#include <QRandomGenerator>
#include <QIcon>


SlidingVerificationDialog::SlidingVerificationDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SlidingVerificationDialog)
{
    ui->setupUi(this);


    //设置图标与标题
    this->setWindowTitle(" ");
    this->setWindowIcon(QIcon(":/images/dog_svg.svg"));
    //设置背景图片
    QPixmap pix(":/images/th.jfif");
    ui->label_background->setPixmap(pix);
    //获取背景图片大小
    int width_background= pix.width();
    int height_background= pix.height();
    ui->horizontalSlider->setMaximum(width_background);//设置进度条最大值为图片宽度

    //随机生成需要截取的图片坐标
    x = QRandomGenerator::global()->bounded(50, width_background-width_copy + 1);
    y = QRandomGenerator::global()->bounded(1,height_background-height_copy + 1);

    // 将抠出区域用空白格子背景代替
    QPixmap pixmap(":/images/1484b88e_E415822_7faf5842.png");
    pixmap.scaled(width_copy,height_copy);
    QLabel *label_Verificat= new QLabel(ui->label_background);
    label_Verificat->setPixmap(pixmap);
    label_Verificat->setGeometry(x,y,width_copy,height_copy);

    // 创建一个新图像，用于存储抠出的区域图片
    QPixmap croppedPixmap = pix.copy(x,y,width_copy,height_copy);
    label_gap= new QLabel(ui->label_background);
    label_gap->setPixmap(croppedPixmap);
    label_gap->setStyleSheet("QLabel { border: 2px solid red; }");
    m_x= 0;//初始化滑块图片坐标为0
    label_gap->setGeometry(m_x,y,width_copy,height_copy);

}

SlidingVerificationDialog::~SlidingVerificationDialog()
{
    delete ui;
}

void SlidingVerificationDialog::on_horizontalSlider_valueChanged(int value)
{
    m_x= value;
    label_gap->move(m_x,y);
}


void SlidingVerificationDialog::on_horizontalSlider_sliderReleased()
{
    //滑动框允许的误差范围
    int min_x= x-5;
    int max_x= x+5;

    //如果验证成功
    if(m_x>=min_x && m_x<=max_x){
        emit sig_result(true);//发送信号到主界面

        close();
    }
    else{
        emit sig_result(false);//发送信号到主界面

        close();
    }

}

